#include "threadpool.hpp"
threadpool::threadpool(size_t threadNum, size_t queSize)
    : _threadNum(threadNum), _threads(), _queSize(queSize), _taskQue(queSize), _isExit(false)
{
}
threadpool::~threadpool()
{
}
void threadpool::start()
{
    for (int i = 0; i != _threadNum; i++)
    {
        _threads.push_back(thread(&threadpool::doTask, this));
    }
}
void threadpool::stop()
{
    while (!_taskQue.empty())
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    _isExit = true;
    _taskQue.wakeup();
    for (auto &t : _threads)
    {
        t.join();
    }
}
void threadpool::addTask(Task *ptask)
{
    if (ptask)
    {
        _taskQue.push(ptask);
    }
}
Task *threadpool::getTask()
{
    return _taskQue.pop();
}
void threadpool::doTask()
{
    while (!_isExit)
    {
        Task *ptask = getTask();
        if (ptask)
        {
            ptask->process();
        }
        else
        {
            cout << "ptask == nullptr" << endl;
        }
    }
}